package com.atung.parking.service.impl;

import com.atung.parking.config.auth.AuthUserMangerContext;
import com.atung.parking.config.result.ApiResult;
import com.atung.parking.entity.BusLicensePlate;
import com.atung.parking.mapper.BusLicensePlateMapper;
import com.atung.parking.request.ByIdRequest;
import com.atung.parking.request.save.SaveOrUpdateLicensePlateRequest;
import com.atung.parking.service.LicensePlateService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * @author Administrator
 */
@Service
public class LicensePlateServiceImpl implements LicensePlateService {

    @Resource
    private BusLicensePlateMapper busLicensePlateMapper;

    @Override
    public ApiResult saveLicensePlateService(SaveOrUpdateLicensePlateRequest request) {
        Long userId = AuthUserMangerContext.getAuthUserEntity().getUserId();

        if (Objects.nonNull(request.getId())) {
            BusLicensePlate busLicensePlate = busLicensePlateMapper.selectById(request.getId());
            if (Objects.isNull(busLicensePlate)
                    && busLicensePlate.getUserId() != userId) {
                return ApiResult.fail("数据不存在");
            }
            busLicensePlate.setLicenseNumber(request.getLicensePlate());
            busLicensePlateMapper.updateById(busLicensePlate);
            return ApiResult.ok();
        }
        BusLicensePlate busLicensePlate = new BusLicensePlate();
        busLicensePlate.setLicenseNumber(request.getLicensePlate());
        busLicensePlate.setUserId(userId);
        QueryWrapper<BusLicensePlate> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        List<BusLicensePlate> busLicensePlates = busLicensePlateMapper.selectList(queryWrapper);
        if (CollectionUtils.isEmpty(busLicensePlates)) {
            busLicensePlate.setType(0);
        } else {
            long count = busLicensePlates.stream().filter(item -> item.getLicenseNumber().equals(request.getLicensePlate())).count();
            if (count > 0) {
                return ApiResult.fail("新增的车牌号已纯存在");
            }
            busLicensePlate.setType(1);
        }
        busLicensePlateMapper.insert(busLicensePlate);
        return ApiResult.ok();
    }

    @Override
    public List<BusLicensePlate> queryLicenseNameService(String licenseNumber) {
        QueryWrapper<BusLicensePlate> queryWrapper = new QueryWrapper<>();

        return null;
    }

    @Override
    public List<BusLicensePlate> queryLicensePlateListService() {
        QueryWrapper<BusLicensePlate> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", AuthUserMangerContext.getAuthUserEntity().getUserId());
        queryWrapper.orderByAsc("type");
        queryWrapper.orderByDesc("creator_time");
        List<BusLicensePlate> busLicensePlates = busLicensePlateMapper.selectList(queryWrapper);
        return busLicensePlates;
    }

    @Override
    public ApiResult setDefaultLicensePlateServcie(ByIdRequest request) {

        QueryWrapper<BusLicensePlate> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", AuthUserMangerContext.getAuthUserEntity().getUserId());
        queryWrapper.eq("id", request.getId());
        BusLicensePlate busLicensePlate = busLicensePlateMapper.selectOne(queryWrapper);
        if (Objects.nonNull(busLicensePlate)) {
            busLicensePlate.setType(0);
            busLicensePlate.setUserId(AuthUserMangerContext.getAuthUserEntity().getUserId());
            busLicensePlateMapper.updateById(busLicensePlate);
            QueryWrapper<BusLicensePlate> updateTypeWrapper = new QueryWrapper<>();
            updateTypeWrapper.eq("user_id", AuthUserMangerContext.getAuthUserEntity().getUserId());
            updateTypeWrapper.ne("id", request.getId());

            List<BusLicensePlate> busLicensePlates = busLicensePlateMapper.selectList(updateTypeWrapper);
            busLicensePlates.stream().forEach(item -> {
                item.setType(1);
                busLicensePlateMapper.updateById(item);
            });
        }
        return ApiResult.ok();
    }

    @Override
    public ApiResult deleteLicensePlateService(ByIdRequest request) {
        QueryWrapper<BusLicensePlate> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", AuthUserMangerContext.getAuthUserEntity().getUserId());
        queryWrapper.eq("id", request.getId());
        List<BusLicensePlate> busLicensePlates = busLicensePlateMapper.selectList(queryWrapper);
        Optional<BusLicensePlate> first = busLicensePlates.stream().filter(item -> item.getId().equals(request.getId())).findFirst();
        if (!first.isPresent()) {
            return ApiResult.fail("数据不存在");
        }
        BusLicensePlate busLicensePlate = first.get();
        busLicensePlateMapper.deleteById(busLicensePlate.getId());
        if (busLicensePlate.getType() == 0) {

            Optional<BusLicensePlate> first1 = busLicensePlates.stream().filter(item -> !item.getId().equals(request.getId())).findFirst();
            //随机
            if (first1.isPresent()) {
                BusLicensePlate busLicensePlate1 = first1.get();
                busLicensePlate1.setType(0);
                busLicensePlateMapper.updateById(busLicensePlate1);
                return ApiResult.ok();
            }
        }
        return ApiResult.ok();
    }

    @Override
    public List<BusLicensePlate> queryUseByUserId(Long userId) {

        List<BusLicensePlate> busLicensePlates= busLicensePlateMapper.queryUseByUserId(userId);
        return busLicensePlates;
    }

}
